*/ public function register(): array { return [ T_CONST, ]; } /** * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint * @param int $constantPointer */ public function process(File $phpcsFile, $constantPointer): void { $tokens = $phpcsFile->getTokens(); if (count($tokens[$constantPointer]['conditions']) === 0) { return; } /** @var int $classPointer */ $classPointer = array_keys($tokens[$constantPointer]['conditions'])[count($tokens[$constantPointer]['conditions']) - 1]; if (!in_array($tokens[$classPointer]['code'], Tokens::$ooScopeTokens, true)) { return; } $visibilityPointer = TokenHelper::findPreviousEffective($phpcsFile, $constantPointer - 1); if ($tokens[$visibilityPointer]['code'] === T_FINAL) { $visibilityPointer = TokenHelper::findPreviousEffective($phpcsFile, $visibilityPointer - 1); } if (in_array($tokens[$visibilityPointer]['code'], [T_PUBLIC, T_PROTECTED, T_PRIVATE], true)) { return; } $equalSignPointer = TokenHelper::findNext($phpcsFile, T_EQUAL, $constantPointer + 1); $namePointer = TokenHelper::findPreviousEffective($phpcsFile, $equalSignPointer - 1); $message = sprintf( 'Constant %s::%s visibility missing.', ClassHelper::getFullyQualifiedName($phpcsFile, $classPointer), $tokens[$namePointer]['content'], ); if ($this->fixable) { $fix = $phpcsFile->addFixableError($message, $constantPointer, self::CODE_MISSING_CONSTANT_VISIBILITY); if ($fix) { $phpcsFile->fixer->beginChangeset(); FixerHelper::addBefore($phpcsFile, $constantPointer, 'public '); $phpcsFile->fixer->endChangeset(); } } else { $phpcsFile->addError($message, $constantPointer, self::CODE_MISSING_CONSTANT_VISIBILITY); } } }